import type Main from "../main";
import type { IPackAntvX6 } from "../inter";
import { reactive, ref } from "vue";
import NodelistEl from "../component/nodelist.vue";


export default (main:Main) => {
    const enabled = ref(true)

    const list = reactive<IPackAntvX6.NodelistItem[]>([]);
    //
    const add = (d:IPackAntvX6.NodelistItem) => {
        list.push(d);
    }
    //
    const setList = (l:IPackAntvX6.NodelistItem[] = []) => {
        list.length = 0;
        list.push(...l)
    }
    //
    const getList = () => {
        return list;
    }

    const setNodeListEl = (el:any) => {
        result.Nodelist = el;
    }
    //
    const find = (fn:(n:any) => boolean) => {
        const t = [...list];
        for(const d of t){
            if(d.children){
                t.push(...d.children)
            }
            if(fn(d)){
                return d;
            }
        }
    }
    //
    const result = {
        enabled,
        Nodelist:NodelistEl,
        props:{list,main},
        setList,
        getList,
        add,
        find,
        setNodeListEl
    }
    //
    return result
}